* This is the master dofile for O'Dea and Sturrock (2021) *

clear all
set more off
set maxvar 30000

* To use these dofiles to replicate outputs, replace the pathways in the globals
* defined below with the pathways to where these raw datasets are saved and then
* run this dofile. *

********************************************************************************

*** Set globals ***

* This global defines the location of the other dofiles
global dofiles "P:\pensions\Retirement Saving Consortium\2016\Survival\ODeaSturrock\ReStatReplication FOR RELEASE"

* These globals set the location for saving final and temporary datasets *
global savedata "P:\pensions\Retirement Saving Consortium\2016\Survival\ODeaSturrock\ReStatReplication FOR RELEASE\Data"
global temp "Y:\David s"

* This global sets the location of the downloaded ONS life table data *
global onsdata "P:\pensions\Retirement Saving Consortium\2016\Survival\ODeaSturrock\ReStatReplication FOR RELEASE\ONS data"

* The following globals select the various ELSA datasets that are available to download from the UK Data Service website. *
* Core files *
global core1  "M:\ELSA\data\wave_1_core_data_v3.dta"
global core2  "M:\ELSA\data\wave_2_core_data_v4.dta"
global core3  "M:\ELSA\data\wave_3_elsa_data_v4.dta"
global core4  "M:\ELSA\data\wave_4_elsa_data_v3.dta"
global core5  "M:\ELSA\data\wave_5_elsa_data_v4.dta"
global core6  "M:\ELSA\data\wave_6_elsa_data_v2.dta"
global core7  "M:\ELSA\data\wave_7_elsa_data.dta"
* IFS derived variables *
global dv1    "M:\ELSA\data\wave_1_ifs_derived_variables.dta"
global dv2    "M:\ELSA\data\wave_2_ifs_derived_variables.dta"
global dv3    "M:\ELSA\data\wave_3_ifs_derived_variables.dta"
global dv4    "M:\ELSA\data\wave_4_ifs_derived_variables.dta"
global dv5    "M:\ELSA\data\wave_5_ifs_derived_variables.dta"
global dv6    "M:\ELSA\data\wave_6_ifs_derived_variables.dta"
global dv7    "M:\ELSA\data\wave_7_ifs_derived_variables.dta"
* IFS financial derived variables *
global findv1 "M:\ELSA\data\wave_1_financial_derived_variables.dta"
global findv2 "M:\ELSA\data\wave_2_financial_derived_variables.dta"
global findv3 "M:\ELSA\data\wave_3_financial_derived_variables.dta"
global findv4 "M:\ELSA\data\wave_4_financial_derived_variables.dta"
global findv5 "M:\ELSA\data\wave_5_financial_derived_variables.dta"
global findv6 "M:\ELSA\data\wave_6_financial_derived_variables.dta"
global findv7 "M:\ELSA\data\wave_7_financial_derived_variables.dta"
* IFS pension wealth variables (only available waves 2 to 5) *
global penv2 "M:\ELSA\data\wave_2_pension_wealth.dta"
global penv3 "M:\ELSA\data\wave_3_pension_wealth.dta"
global penv4 "M:\ELSA\data\wave_4_pension_wealth.dta"
global penv5 "M:\ELSA\data\wave_5_pension_wealth.dta"
* Index file *
global index  "M:\ELSA\data\index_file_wave_0-wave_5_v2.dta"

* The following global sets the location of the linked mortality dataset held at IFS *
global mortality "M:\ELSA\data\mortality"

* The following sets the location of the cleaned mortality record held at IFS *
global derivedmortality "M:\ELSA\data\mortality\CombinedMortalityRecord\CombinedMortalityRecord.dta"

* The following global sets the location of the household grid dataset held at IFS *
global hhgrid "M:\ELSA\data\householdgrids"

* The following global sets the location of the exact date of birth dataset held at IFS *
global dob    "M:\ELSA\data\DOB\full\ELSA_W1-7_dob.dta"

* The following global sets the location of the exact interview date dataset held at IFS *
global intdat "M:\ELSA\data\InterviewDate\ELSAInterviewDateAllWaves.dta"

* this global sets the location of the health dynamics dofiles
global healthdo	"P:\pensions\Retirement Saving Consortium\2016\Survival\ODeaSturrock\ReStatReplication FOR RELEASE\HealthDynamics"

* this dofile sets the location of the intermediate and final health dynamics datasets
global healthsave "P:\pensions\Retirement Saving Consortium\2016\Survival\ODeaSturrock\ReStatReplication FOR RELEASE\Health data"

* The following global sets the location of the dataset created by the health cleaning files *
global healthdata "P:\pensions\Retirement Saving Consortium\2016\Survival\ODeaSturrock\ReStatReplication FOR RELEASE\Data\Health data\derived health variables long_with_W7.dta" 

* The following sets the location for outputs *
global output "P:\pensions\Retirement Saving Consortium\2016\Survival\ODeaSturrock\ReStatReplication FOR RELEASE\Outputs"

* The following sets the location to save inputs into the fortran model *
global fortran "P:\pensions\Retirement Saving Consortium\2016\Survival\ODeaSturrock\ReStatReplication FOR RELEASE\Fortran inputs"

* The following sets the location to save the inputs as re-formatted for the fortran model *
global fortranInputs "C:\Users\david_s\Dropbox\Annuities adapted code\FortranCode\input\finalForRep"

* The following sets the locations for the outputs of the fortran model *
global simMasterPath "C:\Users\david_s\Dropbox\Annuities adapted code\FortranOutput\finalRepDS"


********************************************************************************

*** Run dofiles ***

** Part  1
* Preparation of data and non-model paper outputs *
run "$dofiles\00 CombinedMortalityRecord"
run "$dofiles\01 ConstructHealthVars"
run "$dofiles\02 Basedata"
run "$dofiles\03 Fitted ELSA survival curves"
run "$dofiles\04 Descriptives.do"
* Creation of inputs for fortran lifecycle model (includes Fig 5)
run "$dofiles\05a Make parameters.do"
	// calls "$dofiles\05.1 calc state pension age.do"
run "$dofiles\05b Make parameters unscaled.do"
	// calls "$dofiles\05.1 calc state pension age.do"
	
** Part 2
* Analysing fortran lifecycle model outputs *
run "$dofiles\06 Gather fortran outputs.do"
run "$dofiles\07 Analyse fortran outputs.do"
* Some additional content for the appendix
run "$dofiles\08 Appendix.do"
